// Store
import engine from 'store'

export const cookieStorage = engine.createStore([require('store/storages/cookieStorage')], []);
export const sessionStorage = engine.createStore([require('store/storages/sessionStorage')], []);

// MD5
import Crypto from 'crypto'

export function md5(str) {
    let md5 = Crypto.createHash('md5');
    md5.update(str);
    return md5.digest('hex');
}

// 导出xls数据
export function export2Excel(tHeader, filterVal, tabledata, filename) {
    require.ensure([], () => {
        const {export_json_to_excel} = require('@/vendor/Export2Excel');
        const data = tabledata.map(v => filterVal.map(j => v[j]));
        export_json_to_excel(tHeader, data, filename)
    })
}

// 日期范围配置
export const daterangeOptions = {
    shortcuts: [{
        text: '昨天',
        onClick(picker) {
            const end = new Date();
            end.setTime(end.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', [end, end]);
        }
    }, {
        text: '今天',
        onClick(picker) {
            const start = new Date();
            picker.$emit('pick', [start, start]);
        }
    }, {
        text: '最近7天',
        onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
        }
    }, {
        text: '最近30天',
        onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
        }
    }]
};

/**
 * 下拉选择数据格式化
 * @param data 待转换数据
 * @param label 目标选中的值label
 * @param value 目标选中的值value
 */
export function formatSelectOption(data, label = 'name', value = 'code') {
    let ret = [];
    data.forEach(item => {
        ret.push(Object.assign({}, item, {
            label: item[label],
            value: item[value]
        }));
    });
    return ret;
}


